﻿namespace AnyOffice.WebControls
{
    using System;
    using System.ComponentModel;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Web.Caching;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [DefaultProperty("Text"), ToolboxData("<{0}:SqlPagerControl runat=\"server\"></{0}:SqlPagerControl>")]
    public class SqlPagerControl : WebControl, INamingContainer
    {
        private Control _controlToPaginate = null;
        private PagedDataSource _dataSource = null;
        private int _GotoPage;
        private string CurrentPageText = "第<font color={0}>{1}</font>页 共<font color={0}>{2}</font>页 ";
        private string NoPageSelectedText = "没有信息";

        public event PageChangedEventHandler PageIndexChanged;

        public SqlPagerControl()
        {
            this.Font.Name = "verdana";
            this.Font.Size = FontUnit.Point(9);
            this.BackColor = Color.Gainsboro;
            this.ForeColor = Color.Black;
            this.IndexColor = Color.Black;
            this.BorderStyle = BorderStyle.Outset;
            this.BorderWidth = Unit.Parse("1px");
            this.PagingMode = AnyOffice.WebControls.PagingMode.NonCached;
            this.PagerStyle = AnyOffice.WebControls.PagerStyle.Character;
            this.CurrentPageIndex = 0;
            this.SelectCommand = "";
            this.ConnectionString = "";
            this.ItemsPerPage = 15;
            this.TotalPages = -1;
            this.CacheDuration = 60;
            this.SortMode = AnyOffice.WebControls.SortMode.DESC;
            this.IsShowCurrentPageInfo = true;
            this.NumberPageBegin = 0;
            this.NotVisitCssClass = "";
            this.VisitedCssClass = "";
            this.PrevNextCssClass = "";
            this.IsShowPervNext = true;
            this.ConnectionString = ConfigurationSettings.AppSettings["CONN_STR"];
        }

        private void AdjustSelectCommand(bool addCustomSortInfo)
        {
            object[] args = new object[] { this.TableName, this.FieldKey, this.CurrentPageIndex + 1, this.ItemsPerPage, this.Fields, this.SortField + ((this.SortMode == AnyOffice.WebControls.SortMode.DESC) ? " DESC" : " ASC"), this.SQLCondition };
            this.SelectCommand = string.Format("exec sp_PageView '{0}','{1}',{2},{3},'{4}','{5}','{6}'", args);
        }

        private AnyOffice.WebControls.SortMode AlterSortMode(AnyOffice.WebControls.SortMode mode)
        {
            if (mode == AnyOffice.WebControls.SortMode.DESC)
            {
                mode = AnyOffice.WebControls.SortMode.ASC;
                return mode;
            }
            mode = AnyOffice.WebControls.SortMode.DESC;
            return mode;
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            TextBox box = (TextBox) this.FindControl("txbPage");
            if (box.Text.Length > 0)
            {
                int num = Convert.ToInt32(box.Text.Trim());
                if (((num - 1) >= 0) && ((num - 1) < this.TotalPages))
                {
                    this.GoToPage(num - 1);
                }
            }
        }

        private void BuildCharacterUI(TableCell cell)
        {
            bool flag = (this.CurrentPageIndex >= 0) && (this.CurrentPageIndex <= (this.TotalPages - 1));
            bool flag2 = this.CurrentPageIndex > 0;
            bool flag3 = this.CurrentPageIndex < (this.TotalPages - 1);
            LinkButton child = new LinkButton();
            child.ID = "First";
            child.CausesValidation = false;
            child.Click += new EventHandler(this.first_Click);
            child.ToolTip = "首页";
            child.Font.Name = "webdings";
            child.Text = "9";
            child.Enabled = flag && flag2;
            cell.Controls.Add(child);
            cell.Controls.Add(new LiteralControl("&nbsp;"));
            LinkButton button2 = new LinkButton();
            button2.ID = "Prev";
            button2.CausesValidation = false;
            button2.Click += new EventHandler(this.prev_Click);
            button2.Font.Name = "webdings";
            button2.ToolTip = "前页";
            button2.Text = "3";
            button2.Enabled = flag && flag2;
            cell.Controls.Add(button2);
            cell.Controls.Add(new LiteralControl("&nbsp;"));
            LinkButton button3 = new LinkButton();
            button3.ID = "Next";
            button3.CausesValidation = false;
            button3.Click += new EventHandler(this.next_Click);
            button3.Font.Name = "webdings";
            button3.ToolTip = "后页";
            button3.Text = "4";
            button3.Enabled = flag && flag3;
            cell.Controls.Add(button3);
            cell.Controls.Add(new LiteralControl("&nbsp;"));
            LinkButton button4 = new LinkButton();
            button4.ID = "Last";
            button4.CausesValidation = false;
            button4.Click += new EventHandler(this.last_Click);
            button4.Font.Name = "webdings";
            button4.ToolTip = "尾页";
            button4.Text = ":";
            button4.Enabled = flag && flag3;
            cell.Controls.Add(button4);
        }

        private void BuildControlHierarchy()
        {
            Table child = new Table();
            child.Font.Name = this.Font.Name;
            child.Font.Size = this.Font.Size;
            child.BorderStyle = this.BorderStyle;
            child.BorderWidth = this.BorderWidth;
            child.BorderColor = this.BorderColor;
            child.Width = this.Width;
            child.Height = this.Height;
            child.BackColor = this.BackColor;
            child.ForeColor = this.ForeColor;
            TableRow row = new TableRow();
            child.Rows.Add(row);
            if (this.IsShowCurrentPageInfo)
            {
                TableCell cell = new TableCell();
                cell.HorizontalAlign = HorizontalAlign.Right;
                this.BuildCurrentPage(cell);
                row.Cells.Add(cell);
            }
            TableCell cell2 = new TableCell();
            switch (this.PagerStyle)
            {
                case AnyOffice.WebControls.PagerStyle.Character:
                    if (this.IsShowPervNext)
                    {
                        this.BuildPrevUI(cell2);
                    }
                    this.BuildNumberUI(cell2);
                    if (this.IsShowPervNext)
                    {
                        this.BuildNextUI(cell2);
                    }
                    break;

                case AnyOffice.WebControls.PagerStyle.NumericPages:
                    this.BuildNumericPagesUI(cell2);
                    break;
            }
            row.Cells.Add(cell2);
            TableCell cell3 = new TableCell();
            this.BuildGotoPage(cell3);
            row.Cells.Add(cell3);
            this.Controls.Add(child);
            foreach (Control control in this.Controls[0].Controls[0].Controls[0].Controls)
            {
                if (control is LinkButton)
                {
                    LinkButton button = (LinkButton) control;
                    if (button.Text == this.GotoPage.ToString())
                    {
                        button.CssClass = this.VisitedCssClass;
                    }
                }
            }
        }

        private void BuildCurrentPage(TableCell cell)
        {
            Label child = new Label();
            string noPageSelectedText = "";
            if ((this.CurrentPageIndex < 0) || (this.CurrentPageIndex >= this.TotalPages))
            {
                noPageSelectedText = this.NoPageSelectedText;
            }
            else
            {
                noPageSelectedText = string.Format(this.CurrentPageText, new object[] { this.IndexColor.Name, this.CurrentPageIndex + 1, this.TotalPages, this.TotalRecords, this.ItemsPerPage });
            }
            child.Text = noPageSelectedText;
            cell.Controls.Add(child);
        }

        private void BuildGotoPage(TableCell cell)
        {
            DropDownList child = new DropDownList();
            child.ID = "PageList";
            child.AutoPostBack = true;
            child.SelectedIndexChanged += new EventHandler(this.TopPageList_Click);
            child.Font.Name = this.Font.Name;
            child.Font.Size = this.Font.Size;
            child.ForeColor = this.ForeColor;
            if ((this.TotalPages <= 0) || (this.CurrentPageIndex == -1))
            {
                child.Items.Add("无页面");
                child.Enabled = false;
                child.SelectedIndex = 0;
            }
            else
            {
                for (int i = 1; i <= this.TotalPages; i++)
                {
                    int num2 = i - 1;
                    ListItem item = new ListItem(i.ToString(), num2.ToString());
                    child.Items.Add(item);
                }
                child.SelectedIndex = this.CurrentPageIndex;
            }
            if (!this.IsShowPervNext)
            {
                cell.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
            }
            cell.Controls.Add(child);
        }

        private void BuildNextUI(TableCell cell)
        {
            bool flag = (this.CurrentPageIndex >= 0) && (this.CurrentPageIndex <= (this.TotalPages - 1));
            bool flag2 = this.CurrentPageIndex > 0;
            bool flag3 = this.CurrentPageIndex < (this.TotalPages - 1);
            cell.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
            LinkButton child = new LinkButton();
            child.ID = "Next";
            child.Click += new EventHandler(this.next_Click);
            child.Font.Name = "webdings";
            child.ToolTip = "下一页";
            child.Text = "8";
            child.CssClass = this.PrevNextCssClass;
            child.Enabled = flag && flag3;
            cell.Controls.Add(child);
            cell.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
            LinkButton button2 = new LinkButton();
            button2.ID = "Last";
            button2.Click += new EventHandler(this.last_Click);
            button2.Font.Name = "webdings";
            button2.ToolTip = "末页";
            button2.Text = ":";
            button2.CssClass = this.PrevNextCssClass;
            button2.Enabled = flag && flag3;
            cell.Controls.Add(button2);
            cell.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
        }

        private void BuildNumberUI(TableCell cell)
        {
            bool flag = (this.CurrentPageIndex >= 0) && (this.CurrentPageIndex <= (this.TotalPages - 1));
            bool flag2 = this.CurrentPageIndex > 0;
            bool flag3 = this.CurrentPageIndex < (this.TotalPages - 1);
            bool flag4 = this.CurrentPageIndex == 0;
            bool flag5 = this.CurrentPageIndex == (this.TotalPages - 1);
            cell.Controls.Add(new LiteralControl("&nbsp;"));
            LinkButton child = new LinkButton();
            child.ID = "link_first_0";
            child.ToolTip = "第1页";
            child.Text = "1";
            child.Click += new EventHandler(this.linkfirst_Click);
            child.Enabled = !flag4;
            child.CssClass = this.NotVisitCssClass;
            cell.Controls.Add(child);
            cell.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
            LinkButton button2 = new LinkButton();
            int num = 0;
            if (((this.CurrentPageIndex % 10) > 0) && (this.CurrentPageIndex < (this.TotalPages - 1)))
            {
                num = ((this.CurrentPageIndex / 10) + 1) * 10;
            }
            if ((this.CurrentPageIndex % 10) == 0)
            {
                num = ((this.CurrentPageIndex / 10) - 1) * 10;
            }
            button2.ID = "linkprevten_" + num.ToString();
            button2.ToolTip = "前10页";
            button2.Text = "...";
            button2.Click += new EventHandler(this.linknumber_Click);
            button2.Enabled = (this.CurrentPageIndex - 1) >= 10;
            cell.Controls.Add(button2);
            cell.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
            int num2 = 0;
            if (((this.CurrentPageIndex + 1) % 10) == 0)
            {
                this.NumberPageBegin = this.CurrentPageIndex + 1;
            }
            else if (((this.CurrentPageIndex + 1) % 10) == 1)
            {
                if (this.CurrentPageIndex >= 10)
                {
                    this.NumberPageBegin = this.CurrentPageIndex - 10;
                }
                else
                {
                    this.NumberPageBegin = 0;
                }
            }
            else
            {
                if (((this.CurrentPageIndex / 10) > 0) && ((this.CurrentPageIndex / 10) < (this.TotalPages / 10)))
                {
                    this.NumberPageBegin = (this.CurrentPageIndex / 10) * 10;
                }
                if (((this.CurrentPageIndex / 10) > 0) && ((this.CurrentPageIndex / 10) == (this.TotalPages / 10)))
                {
                    this.NumberPageBegin = (this.CurrentPageIndex / 10) * 10;
                }
            }
            for (int i = this.NumberPageBegin; (i < this.TotalPages) && (num2 < 10); i++)
            {
                LinkButton button3 = new LinkButton();
                button3.ID = "midLnk_" + i.ToString();
                int num5 = i + 1;
                button3.Text = num5.ToString();
                button3.Click += new EventHandler(this.linknumber_Click);
                button3.ToolTip = "第" + ((i + 1)).ToString() + "页";
                button3.CssClass = this.NotVisitCssClass;
                cell.Controls.Add(button3);
                cell.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
                num2++;
            }
            LinkButton button4 = new LinkButton();
            int num4 = 0;
            if (((this.CurrentPageIndex % 10) == 0) && (this.CurrentPageIndex > 0))
            {
                num4 = (((this.CurrentPageIndex / 10) - 1) * 10) + 9;
            }
            if (((this.CurrentPageIndex % 10) == 0) && (this.CurrentPageIndex == 0))
            {
                num4 = ((this.CurrentPageIndex / 10) * 10) + 9;
            }
            if ((this.CurrentPageIndex % 10) > 0)
            {
                num4 = (((this.CurrentPageIndex / 10) + 1) * 10) + 9;
            }
            button4.ID = "linknextten_" + num4.ToString();
            button4.ToolTip = "后10页";
            button4.Text = "...";
            button4.Click += new EventHandler(this.linknumber_Click);
            button4.Enabled = num4 <= this.TotalPages;
            cell.Controls.Add(button4);
            cell.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
            LinkButton button5 = new LinkButton();
            button5.ID = "linkfirst_" + ((this.TotalPages - 1)).ToString();
            button5.ToolTip = "第" + this.TotalPages.ToString() + "页";
            button5.Text = this.TotalPages.ToString();
            button5.Click += new EventHandler(this.linklast_Click);
            button5.Enabled = !flag5;
            button5.CssClass = this.NotVisitCssClass;
            cell.Controls.Add(button5);
        }

        private void BuildNumericPagesUI(TableCell cell)
        {
            DropDownList child = new DropDownList();
            child.ID = "PageList";
            child.AutoPostBack = true;
            child.SelectedIndexChanged += new EventHandler(this.PageList_Click);
            child.Font.Name = this.Font.Name;
            child.Font.Size = this.Font.Size;
            child.ForeColor = this.ForeColor;
            if ((this.TotalPages <= 0) || (this.CurrentPageIndex == -1))
            {
                child.Items.Add("无页面");
                child.Enabled = false;
                child.SelectedIndex = 0;
            }
            else
            {
                for (int i = 1; i <= this.TotalPages; i++)
                {
                    int num2 = i - 1;
                    ListItem item = new ListItem(i.ToString(), num2.ToString());
                    child.Items.Add(item);
                }
                child.SelectedIndex = this.CurrentPageIndex;
            }
            cell.Controls.Add(child);
        }

        private void BuildPrevUI(TableCell cell)
        {
            bool flag = (this.CurrentPageIndex >= 0) && (this.CurrentPageIndex <= (this.TotalPages - 1));
            bool flag2 = this.CurrentPageIndex > 0;
            bool flag3 = this.CurrentPageIndex < (this.TotalPages - 1);
            LinkButton child = new LinkButton();
            child.ID = "First";
            child.Click += new EventHandler(this.first_Click);
            child.Font.Name = "webdings";
            child.ToolTip = "首页";
            child.Text = "9";
            child.CssClass = this.PrevNextCssClass;
            child.Enabled = flag && flag2;
            cell.Controls.Add(child);
            cell.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
            LinkButton button2 = new LinkButton();
            button2.ID = "Prev";
            button2.Click += new EventHandler(this.prev_Click);
            button2.Font.Name = "webdings";
            button2.ToolTip = "上一页";
            button2.Text = "7";
            button2.CssClass = this.PrevNextCssClass;
            button2.Enabled = flag && flag2;
            cell.Controls.Add(button2);
            cell.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
        }

        private VirtualRecordCount CalculateVirtualRecordCount()
        {
            VirtualRecordCount count = new VirtualRecordCount();
            count.RecordCount = this.GetQueryVirtualCount();
            count.RecordsInLastPage = this.ItemsPerPage;
            int num = count.RecordCount / this.ItemsPerPage;
            int num2 = count.RecordCount % this.ItemsPerPage;
            if (num2 > 0)
            {
                num++;
            }
            count.PageCount = num;
            if (num2 > 0)
            {
                count.RecordsInLastPage = num2;
            }
            return count;
        }

        private void ChangeLnkBtnCssClass(LinkButton link)
        {
            foreach (Control control in this.Controls)
            {
                if ((control is LinkButton) && (((LinkButton) control).ClientID.IndexOf("midLnk") > 0))
                {
                    ((LinkButton) control).Attributes.Remove("class");
                }
            }
            link.Attributes.Add("class", this.VisitedCssClass);
        }

        public void ClearCache()
        {
            if (this.PagingMode == AnyOffice.WebControls.PagingMode.Cached)
            {
                this.Page.Cache.Remove(this.CacheKeyName);
            }
        }

        protected override void CreateChildControls()
        {
            this.Controls.Clear();
            base.ClearChildViewState();
            this.BuildControlHierarchy();
        }

        public override void DataBind()
        {
            object[] args = new object[] { this.TableName, this.FieldKey, this.CurrentPageIndex + 1, this.ItemsPerPage, this.Fields, this.SortField + ((this.SortMode == AnyOffice.WebControls.SortMode.DESC) ? " DESC" : " ASC"), this.SQLCondition };
            this.SelectCommand = string.Format("exec sp_PageView '{0}','{1}',{2},{3},'{4}','{5}','{6}'", args);
            base.DataBind();
            base.ChildControlsCreated = false;
            if (this.ControlToPaginate != "")
            {
                this._controlToPaginate = this.Page.FindControl(this.ControlToPaginate);
                if (((this._controlToPaginate != null) && (((this._controlToPaginate is BaseDataList) || (this._controlToPaginate is ListControl)) || (this._controlToPaginate is CompositeDataBoundControl))) && ((this.ConnectionString != "") && (this.SelectCommand != "")))
                {
                    if (this.PagingMode == AnyOffice.WebControls.PagingMode.Cached)
                    {
                        this.FetchAllData();
                    }
                    else
                    {
                        this.FetchPageData();
                    }
                    BaseDataList list = null;
                    ListControl control = null;
                    if (this._controlToPaginate is BaseDataList)
                    {
                        list = (BaseDataList) this._controlToPaginate;
                        list.DataSource = this._dataSource;
                        list.DataBind();
                    }
                    else if (this._controlToPaginate is ListControl)
                    {
                        control = (ListControl) this._controlToPaginate;
                        control.Items.Clear();
                        control.DataSource = this._dataSource;
                        control.DataBind();
                    }
                    else if (this._controlToPaginate is CompositeDataBoundControl)
                    {
                        GridView view = (GridView) this._controlToPaginate;
                        view.DataSource = this._dataSource;
                        view.DataBind();
                    }
                }
            }
        }

        private void FetchAllData()
        {
            this.SelectCommand = string.Format("exec p_Pagination '{0}','{1}','{2}',{3},{4},{5},{6},'{7}'", new object[] { this.TableName, this.Fields, this.SortField, this.ItemsPerPage, this.CurrentPageIndex + 1, 0, (this.SortMode == AnyOffice.WebControls.SortMode.DESC) ? "1" : "0", this.SQLCondition });
            DataTable dataTable = (DataTable) this.Page.Cache[this.CacheKeyName];
            if (dataTable == null)
            {
                this.AdjustSelectCommand(true);
                SqlDataAdapter adapter = new SqlDataAdapter(this.SelectCommand, this.ConnectionString);
                dataTable = new DataTable();
                adapter.Fill(dataTable);
                this.Page.Cache.Insert(this.CacheKeyName, dataTable, null, DateTime.Now.AddSeconds((double) this.CacheDuration), Cache.NoSlidingExpiration);
            }
            if (this._dataSource == null)
            {
                this._dataSource = new PagedDataSource();
            }
            this._dataSource.DataSource = dataTable.DefaultView;
            this._dataSource.PageSize = this.ItemsPerPage;
            this.TotalPages = this._dataSource.PageCount;
            this.TotalRecords = this._dataSource.Count;
            this.ValidatePageIndex();
            if (this.CurrentPageIndex == -1)
            {
                this._dataSource = null;
            }
            else
            {
                this._dataSource.CurrentPageIndex = this.CurrentPageIndex;
            }
        }

        private void FetchPageData()
        {
            this.AdjustSelectCommand(false);
            VirtualRecordCount countInfo = this.CalculateVirtualRecordCount();
            this.TotalPages = countInfo.PageCount;
            this.TotalRecords = countInfo.RecordCount;
            this.ValidatePageIndex();
            if (this.CurrentPageIndex == -1)
            {
                this.CurrentPageIndex = 0;
            }
            SqlCommand selectCommand = this.PrepareCommand(countInfo);
            if (selectCommand != null)
            {
                SqlDataAdapter adapter = new SqlDataAdapter(selectCommand);
                DataTable dataTable = new DataTable("soursetable");
                adapter.Fill(dataTable);
                if (this._dataSource == null)
                {
                    this._dataSource = new PagedDataSource();
                }
                this._dataSource.CurrentPageIndex = 0;
                this._dataSource.PageSize = this.ItemsPerPage;
                this._dataSource.VirtualCount = countInfo.RecordCount;
                this._dataSource.DataSource = dataTable.DefaultView;
            }
        }

        private void first_Click(object sender, EventArgs e)
        {
            this.GoToPage(0);
        }

        private int GetQueryVirtualCount()
        {
            string str = "";
            if (this.SQLCondition != "")
            {
                str = string.Format("select count(*) from {0} as xxa where {1}", this.TableName, this.SQLCondition);
            }
            else
            {
                str = string.Format("select count(*) from {0} xxa", this.TableName);
            }
            string cmdText = str.Replace("''%", "'%").Replace("%''", "%'");
            SqlConnection connection = new SqlConnection(this.ConnectionString);
            SqlCommand command = new SqlCommand(cmdText, connection);
            command.Connection.Open();
            int num = (int) command.ExecuteScalar();
            command.Connection.Close();
            return num;
        }

        public void GoToPage(int pageIndex)
        {
            this.GotoPage = pageIndex + 1;
            PageChangedEventArgs e = new PageChangedEventArgs();
            e.OldPageIndex = this.CurrentPageIndex;
            e.NewPageIndex = pageIndex;
            this.CurrentPageIndex = pageIndex;
            this.OnPageIndexChanged(e);
            this.DataBind();
        }

        private void last_Click(object sender, EventArgs e)
        {
            this.GoToPage(this.TotalPages - 1);
        }

        private void linkfirst_Click(object sender, EventArgs e)
        {
            this.GoToPage(0);
        }

        private void linklast_Click(object sender, EventArgs e)
        {
            this.GoToPage(this.TotalPages - 1);
        }

        private void linknumber_Click(object sender, EventArgs e)
        {
            LinkButton button = (LinkButton) sender;
            string iD = button.ID;
            int startIndex = iD.IndexOf("_") + 1;
            int pageIndex = Convert.ToInt32(iD.Substring(startIndex, iD.Length - startIndex));
            button.CssClass = this.VisitedCssClass;
            this.GoToPage(pageIndex);
        }

        private void next_Click(object sender, EventArgs e)
        {
            this.GoToPage(this.CurrentPageIndex + 1);
        }

        protected virtual void OnPageIndexChanged(PageChangedEventArgs e)
        {
            if (this.PageIndexChanged != null)
            {
                this.PageIndexChanged(this, e);
            }
        }

        private void PageList_Click(object sender, EventArgs e)
        {
            DropDownList list = (DropDownList) sender;
            int pageIndex = Convert.ToInt32(list.SelectedItem.Value);
            this.GoToPage(pageIndex);
        }

        private SqlCommand PrepareCommand(VirtualRecordCount countInfo)
        {
            if (this.SortField == "")
            {
                SqlDataAdapter adapter = new SqlDataAdapter("SET FMTONLY ON;" + this.SelectCommand + ";SET FMTONLY OFF;", this.ConnectionString);
                DataTable dataTable = new DataTable();
                adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
                adapter.Fill(dataTable);
                DataColumn column = null;
                if (dataTable.PrimaryKey.Length > 0)
                {
                    column = dataTable.PrimaryKey[0];
                }
                else
                {
                    column = dataTable.Columns[0];
                }
                this.SortField = column.ColumnName;
            }
            int itemsPerPage = this.ItemsPerPage;
            if (this.CurrentPageIndex == (countInfo.PageCount - 1))
            {
                itemsPerPage = countInfo.RecordsInLastPage;
            }
            string selectCommand = this.SelectCommand;
            return new SqlCommand(selectCommand, new SqlConnection(this.ConnectionString));
        }

        private void prev_Click(object sender, EventArgs e)
        {
            this.GoToPage(this.CurrentPageIndex - 1);
        }

        protected override void Render(HtmlTextWriter output)
        {
            if ((base.Site != null) && base.Site.DesignMode)
            {
                this.CreateChildControls();
            }
            base.Render(output);
        }

        private void TopPageList_Click(object sender, EventArgs e)
        {
            DropDownList list = (DropDownList) sender;
            int pageIndex = Convert.ToInt32(list.SelectedValue);
            this.GoToPage(pageIndex);
        }

        private void ValidatePageIndex()
        {
            if (this.CurrentPageIndex == this.TotalPages)
            {
                this.CurrentPageIndex--;
            }
            if ((this.CurrentPageIndex < 0) || (this.CurrentPageIndex >= this.TotalPages))
            {
                this.CurrentPageIndex = -1;
            }
        }

        [Description("取得或设置数据在缓存中保存多少秒")]
        public int CacheDuration
        {
            get
            {
                return Convert.ToInt32(this.ViewState["CacheDuration"]);
            }
            set
            {
                this.ViewState["CacheDuration"] = value;
            }
        }

        private string CacheKeyName
        {
            get
            {
                return (this.Page.Request.FilePath + "_" + this.UniqueID + "_Data");
            }
        }

        [Description("取得设置数据库连接字符串")]
        public string ConnectionString
        {
            get
            {
                return Convert.ToString(this.ViewState["ConnectionString"]);
            }
            set
            {
                this.ViewState["ConnectionString"] = value;
            }
        }

        [Description("取得设置数据容器的名字")]
        public string ControlToPaginate
        {
            get
            {
                return Convert.ToString(this.ViewState["ControlToPaginate"]);
            }
            set
            {
                this.ViewState["ControlToPaginate"] = value;
            }
        }

        [Description("取得设置当前页面索引")]
        public int CurrentPageIndex
        {
            get
            {
                return Convert.ToInt32(this.ViewState["CurrentPageIndex"]);
            }
            set
            {
                this.ViewState["CurrentPageIndex"] = value;
            }
        }

        [Description("存储过程分页的关键字段(主键)")]
        public string FieldKey
        {
            get
            {
                if (this.ViewState["_FieldKey"] != null)
                {
                    return this.ViewState["_FieldKey"].ToString();
                }
                return "";
            }
            set
            {
                this.ViewState["_FieldKey"] = value;
            }
        }

        [Description("存储过程分页的字段")]
        public string Fields
        {
            get
            {
                if (this.ViewState["_Fields"] != null)
                {
                    return this.ViewState["_Fields"].ToString();
                }
                return "*";
            }
            set
            {
                this.ViewState["_Fields"] = value;
            }
        }

        protected int GotoPage
        {
            get
            {
                return this._GotoPage;
            }
            set
            {
                this._GotoPage = value;
            }
        }

        [Description("指定索引值颜色")]
        public Color IndexColor
        {
            get
            {
                return (Color) this.ViewState["IndexColor"];
            }
            set
            {
                this.ViewState["IndexColor"] = value;
            }
        }

        [Description("是否现实统计信息")]
        public bool IsShowCurrentPageInfo
        {
            get
            {
                return (bool) this.ViewState["ShowCurrentPageInfo"];
            }
            set
            {
                this.ViewState["ShowCurrentPageInfo"] = value;
            }
        }

        [Description("是否现实前后翻页")]
        public bool IsShowPervNext
        {
            get
            {
                return (bool) this.ViewState["_IsShowPervNext"];
            }
            set
            {
                this.ViewState["_IsShowPervNext"] = value;
            }
        }

        [Description("取得设置每页显示的记录数")]
        public int ItemsPerPage
        {
            get
            {
                return Convert.ToInt32(this.ViewState["ItemsPerPage"]);
            }
            set
            {
                this.ViewState["ItemsPerPage"] = value;
            }
        }

        [Description("数字翻页未点击的样式")]
        public string NotVisitCssClass
        {
            get
            {
                return (string) this.ViewState["_NotVisitCssClass"];
            }
            set
            {
                this.ViewState["_NotVisitCssClass"] = value;
            }
        }

        protected int NumberPageBegin
        {
            get
            {
                return Convert.ToInt32(this.ViewState["_NumberPageBegin"]);
            }
            set
            {
                this.ViewState["_NumberPageBegin"] = value;
            }
        }

        [Browsable(false)]
        public int PageCount
        {
            get
            {
                return this.TotalPages;
            }
        }

        [Description("指定导航条样式")]
        public AnyOffice.WebControls.PagerStyle PagerStyle
        {
            get
            {
                return (AnyOffice.WebControls.PagerStyle) this.ViewState["PagerStyle"];
            }
            set
            {
                this.ViewState["PagerStyle"] = value;
            }
        }

        [Description("指定数据是否需要在页面上缓存")]
        public AnyOffice.WebControls.PagingMode PagingMode
        {
            get
            {
                return (AnyOffice.WebControls.PagingMode) this.ViewState["PagingMode"];
            }
            set
            {
                this.ViewState["PagingMode"] = value;
            }
        }

        [Description("前后翻页的样式")]
        public string PrevNextCssClass
        {
            get
            {
                return (string) this.ViewState["_PrevNextCssClass"];
            }
            set
            {
                this.ViewState["_PrevNextCssClass"] = value;
            }
        }

        [Description("取得设置数据库查询语句")]
        public string SelectCommand
        {
            get
            {
                return Convert.ToString(this.ViewState["SelectCommand"]);
            }
            set
            {
                this.ViewState["SelectCommand"] = value;
            }
        }

        [Description("取得设置排序字段，仅在无缓存的模式下可用")]
        public string SortField
        {
            get
            {
                return Convert.ToString(this.ViewState["SortKeyField"]);
            }
            set
            {
                this.ViewState["SortKeyField"] = value;
            }
        }

        [Description("取得设置排序模式")]
        public AnyOffice.WebControls.SortMode SortMode
        {
            get
            {
                return (AnyOffice.WebControls.SortMode) this.ViewState["SortMode"];
            }
            set
            {
                this.ViewState["SortMode"] = value;
            }
        }

        [Description("存储过程分页的条件")]
        public string SQLCondition
        {
            get
            {
                if (this.ViewState["_SQLCondition"] != null)
                {
                    return this.ViewState["_SQLCondition"].ToString();
                }
                return "";
            }
            set
            {
                this.ViewState["_SQLCondition"] = value;
            }
        }

        [Description("存储过程分页的数据表名")]
        public string TableName
        {
            get
            {
                if (this.ViewState["_TableName"] != null)
                {
                    return this.ViewState["_TableName"].ToString();
                }
                return "";
            }
            set
            {
                this.ViewState["_TableName"] = value;
            }
        }

        protected int TotalPages
        {
            get
            {
                return Convert.ToInt32(this.ViewState["TotalPages"]);
            }
            set
            {
                this.ViewState["TotalPages"] = value;
            }
        }

        protected int TotalRecords
        {
            get
            {
                return Convert.ToInt32(this.ViewState["TotalRecords"]);
            }
            set
            {
                this.ViewState["TotalRecords"] = value;
            }
        }

        [Description("数字翻页点击后的样式")]
        public string VisitedCssClass
        {
            get
            {
                return (string) this.ViewState["_VisitedCssClass"];
            }
            set
            {
                this.ViewState["_VisitedCssClass"] = value;
            }
        }

        public delegate void PageChangedEventHandler(object sender, PageChangedEventArgs e);
    }
}

